//
//  WCNPatientAndRecordVC.m
//  WCN
//
//  Created by wsj2015 on 16/1/30.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNPatientAndRecordVC.h"
#import "WCNPatientVC.h"
#import "WCNTypePatientVC.h"
#import "WCDefaultManager.h"
#import "WCNetworkAccessManager.h"
#import "WCPopupLayerManager.h"
#import "WCPatientInforCell.h"
#import "WCNInvitePatientVC.h"
#import "WCInformationDetailVC.h"
#import <YTXAnimations/UIView+YTXAnimateCSS.h>


@interface WCNPatientAndRecordVC ()

@property (nonatomic,  retain) UIView *headView;
@property (nonatomic,  retain) UIButton *footerBtn;

@property (nonatomic,  retain) NSMutableArray *dataArray;



@end

@implementation WCNPatientAndRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configUI];
    
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title =@"随访患者";
    }
    return self;
}
- (void)configUI{
    
  
    [self.view addSubview:self.footerBtn];
    self.tableView.frame =CGRectMake(0, 0, kScreenWidth, kScreenHeight-64-35);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WCPatientInforCell" bundle:nil] forCellReuseIdentifier:@"WCPatientInforCellId"];
    weakSelf_define;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf postPatientContactRecordList];
    }];
    [self.tableView.header beginRefreshing];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   
    [self.footerBtn ytx_bounceAnimtionWithDurationTime:1.0f];
    
}
- (NSMutableArray  *)dataArray{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;
}
- (UIView *)headView{
    if (_headView ==nil) {
        _headView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
        
        UIView *lineView =[[UIView alloc] initWithFrame:CGRectMake(0, 43, kScreenWidth, kOnePX)];
        lineView.backgroundColor = kBorderColor;
        [_headView addSubview:lineView];
        CGFloat btnW =120;
        CGFloat space =40;
        for (int i =0; i<2; i++) {
            UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
            if (i ==0) {
                btn.frame =CGRectMake((kScreenWidth/2) -btnW -space/2, 7, btnW, 30);
                [btn setTitle:@"随访患者" forState:UIControlStateNormal];
                 [btn setBackgroundColor:[UIColor orangeColor]];
                [btn setTintColor:[UIColor whiteColor]];
                
            }else{
                btn.frame =CGRectMake((kScreenWidth/2) +space/2, 7, btnW, 30);
                [btn setTitle:@"住院患者" forState:UIControlStateNormal];
                [btn setBackgroundColor:[UIColor whiteColor]];
                [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
                btn.layer.borderColor =[UIColor orangeColor].CGColor;
                btn.layer.borderWidth = 1;
            }
            
            btn.layer.cornerRadius = 4;
            btn.layer.masksToBounds = YES;
            btn.tag = 200 +i;
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            [_headView addSubview:btn];
        }
        
    }
    return _headView;
}
- (UIButton *)footerBtn{
    if (!_footerBtn) {
        _footerBtn =[UIButton buttonWithType:UIButtonTypeSystem];
        _footerBtn.frame =CGRectMake(30, kScreenHeight -35-65, kScreenWidth -60, 35);
        [_footerBtn setTitle:@"邀请上传患者档案" forState:UIControlStateNormal];
        [_footerBtn setBackgroundColor:HexRGB_WSJ(84, 130, 255)];
        _footerBtn.layer.cornerRadius = 5;
        _footerBtn.layer.masksToBounds= YES;
        
        [_footerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_footerBtn addTarget:self action:@selector(invaiteAddNewContactRecord:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _footerBtn;

}

#pragma mark -请求档案列表
- (void) postPatientContactRecordList{
    
     MBProgressHUD *hud =[WCPopupLayerManager showAnimaitonView];
    weakSelf_define;
    NSInteger nursId =[[WCDefaultManager shareMg] nurseId];
    [[WCNetworkAccessManager sharedMg] POST:kPatientContactRecordList parameters:@{@"nurseId":@(nursId)} success:^(NSDictionary * responseObject) {
        [weakSelf.dataArray removeAllObjects];
        for (NSDictionary  *dict in responseObject[@"data"]) {
            [weakSelf.dataArray addObject:[WCPatientInforList objectWithKeyValues:dict]];
            
        }
        [weakSelf.tableView reloadData];
    } failure:nil netError:nil always:^{
        [hud hide:YES];
        [weakSelf.tableView.header endRefreshing];
    }];
    

}

#pragma mark -Action
- (void)btnAction:(UIButton *)btn{
    if (btn.tag ==200) {
        
    }else{
        WCNTypePatientVC *vc =[WCNTypePatientVC new];
        [self.navigationController pushViewController:vc animated:YES];
    }

}
- (void)invaiteAddNewContactRecord:(id)sender{
    
    [self.navigationController pushViewController:[WCNInvitePatientVC new] animated:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WCPatientInforList *model =self.dataArray[indexPath.row];
    WCPatientInforCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WCPatientInforCellId" forIndexPath:indexPath];
//    defineWeakSelf;
//    cell.motifyBlock = ^(){
//        
//        WCNewPatientInforVC *vc =[WCNewPatientInforVC new];
//        vc.recordId = [model.recordId integerValue];
//        [weakSelf.navigationController pushViewController:vc animated:YES];
//        
//    };
//    cell.updateBlock = ^(){
//        
//        //        WCFocuNurseController *vc =[WCFocuNurseController new];
//        //        [weakSelf.navigationController pushViewController:vc animated:YES];
//        
//        WCUpdatePatientInforVC *vc =[WCUpdatePatientInforVC new];
//        vc.recordId = [model.recordId integerValue];
//        [weakSelf.navigationController pushViewController:vc animated:YES];
//        
//    };
    cell.model = model;
    
    return  cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCPatientInforList *model =self.dataArray[indexPath.row];
    WCInformationDetailVC *vc =[WCInformationDetailVC new];
    vc.recordId = [model.recordId integerValue];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
